package com.lanou3g.musicenjoy.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.entity.Singer;
import com.lanou3g.musicenjoy.adapter.SingerAdapter;
import com.lanou3g.musicenjoy.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/10/20.
 */
public class SingerActivityFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener {
    private List<Singer.ArtistEntity> datas;
    private SingerAdapter adapter;
    private ListView listView;
    private String url;
    private TextView chinaboysinger;
    private ImageView singerBack;
    private RequestQueue requestQueue;
    private View view;
    private InforFragment singerInforActivityFragment;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       view=inflater.inflate(R.layout.activity_singer,null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        requestQueue = VolleySingleton.getInstance(getActivity()).getRequestQueue();
        listView = (ListView) getActivity().findViewById(R.id.singerListView);
        chinaboysinger = (TextView) getActivity().findViewById(R.id.chinaboysinger);
        singerBack = (ImageView) getActivity().findViewById(R.id.singerBack);
        singerBack.setOnClickListener(this);
        adapter = new SingerAdapter(getActivity());
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
        Bundle bundle = getArguments();
        String name = bundle.getString("name");
        int a = bundle.getInt("area", 1);
        int s = bundle.getInt("sex", 1);
        chinaboysinger.setText(name);
        url = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0" +
                "&method=baidu.ting.artist.getList&format=json&offset=0&limit=48&order=1&area=" + a + "&sex=" + s + "";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response != null) {
                    Gson gson = new Gson();
                    Singer result = gson.fromJson(response, Singer.class);
                    if (result != null) {
                        datas = result.getArtist();
                        adapter.initData(datas);
                    }

                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        requestQueue.add(request);

    }

    @Override
    public void onClick(View v) {
        getFragmentManager().popBackStack();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        singerInforActivityFragment=new InforFragment();
        Bundle bundle=new Bundle();
        String a = datas.get(position).getTing_uid();
        bundle.putString("a", a);
        singerInforActivityFragment.setArguments(bundle);
        getFragmentManager()
                .beginTransaction()
                .addToBackStack(null)
                .replace(R.id.container,singerInforActivityFragment)
                .commit();
    }
}